package com.scorcher.ilink;

import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.xmlpull.v1.XmlSerializer;

import android.os.Environment;
import android.util.Log;
import android.util.Xml;

public class XmlCreater implements Runnable{

	//debugging
	public static final boolean D= true;
	public static final String TAG = "Xml Creater";
	
	public void run(){
		File list = new File(Environment.getExternalStorageDirectory(),"/iLink/list.xml");
		File[] listSdCard = Environment.getExternalStorageDirectory().listFiles();
		try{
		FileOutputStream fout = new FileOutputStream(list);
		XmlSerializer serializer = Xml.newSerializer();
		serializer.setOutput(fout,"UTF-8");
		serializer.startDocument(null, false);
		serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
		serializer.startTag(null, "File");
		
		for(File f: listSdCard){
			if(f.isFile()){
			serializer.startTag(null, "Details");
				serializer.startTag(null, "Name");
				serializer.text(f.getName());
				serializer.endTag(null, "Name");

				serializer.startTag(null, "location");
				serializer.text(f.getPath());
				serializer.endTag(null, "location");

				serializer.startTag(null, "size");
				serializer.text(""+f.length());
				serializer.endTag(null, "size");

				serializer.startTag(null, "date");
				SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
				serializer.text(sdf.format(new Date(f.lastModified())));
				serializer.endTag(null, "date");

			serializer.endTag(null, "Details");
			}
		}
		serializer.endTag(null, "File");
		serializer.flush();
		fout.close();
		}
		catch(Exception e)
		{
			if(D) Log.e(TAG, "Error in creation of file",e.getCause());
		}
	}
}
